package org.swimv2.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneMessaggi;
import org.swimv2.controller.GestioneMessaggiRemote;
import org.swimv2.model.Messaggio;
import org.swimv2.model.Persona;



public class VisualizzaMessaggioServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Messaggio mess;
	
	public VisualizzaMessaggioServlet() {
        super();
    }
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Persona p = (Persona) request.getSession().getAttribute("utente");
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneMessaggi.JNDI_NAME);
			GestioneMessaggiRemote manager = (GestioneMessaggiRemote)PortableRemoteObject.narrow(obj,GestioneMessaggiRemote.class);
			
			String mittente= request.getParameter("mittente");
			String destinatario=request.getParameter("destinatario");
			String data = request.getParameter("data");
			String dataParametri[]= data.split(" ");
			String ora[]= dataParametri[3].split(":"); 
			
			if(dataParametri[1].equals("Jan")) dataParametri[1] = "0";
			else if(dataParametri[1].equals("Feb")) dataParametri[1] = "1";
			else if(dataParametri[1].equals("Mar")) dataParametri[1] = "2";
			else if(dataParametri[1].equals("Apr")) dataParametri[1] = "3";
			else if(dataParametri[1].equals("May")) dataParametri[1] = "4";
			else if(dataParametri[1].equals("Jun")) dataParametri[1] = "5";
			else if(dataParametri[1].equals("Jul")) dataParametri[1] = "6";
			else if(dataParametri[1].equals("Aug")) dataParametri[1] = "7";
			else if(dataParametri[1].equals("Sep")) dataParametri[1] = "8";
			else if(dataParametri[1].equals("Oct")) dataParametri[1] = "9";
			else if(dataParametri[1].equals("Nov")) dataParametri[1] = "10";
			else if(dataParametri[1].equals("Dec")) dataParametri[1] = "11";
			
			@SuppressWarnings("deprecation")
			Date dataOra = new Date(
				     Integer.parseInt(dataParametri[5])-1900,
				     Integer.parseInt(dataParametri[1]),
				     Integer.parseInt(dataParametri[2]),
				     Integer.parseInt(ora[0]),
				     Integer.parseInt(ora[1]),
				     Integer.parseInt(ora[2])
					);
			
			mess=manager.getMessaggio(mittente, destinatario, dataOra);
			String corpo=mess.getCorpoMessaggio();
			
			boolean rispondi = true;
			if(mittente.equals(p.getUserID())) { rispondi = false;  }
			
			request.setAttribute("rispondiValue", rispondi);
			request.getSession().setAttribute("messTemp", mess);
			request.getRequestDispatcher("WEB-INF/paginaMessaggio.jsp").
			forward(request, response);			

		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
		
			
		
	}
	

}
